package tcpserver.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * TCP服务器配置类
 * 
 * 从application.yml文件中读取TCP服务器相关配置
 */
@Component
@ConfigurationProperties(prefix = "tcp.server")
@Primary  // 标记为主要Bean
public class TcpServerConfig {
    
    /**
     * 服务器端口
     */
    private int port = 8088;
    
    /**
     * 绑定的IP地址
     */
    private String host = "0.0.0.0";
    
    /**
     * Boss线程数量
     */
    private int bossThreads = 1;
    
    /**
     * Worker线程数量
     */
    private int workerThreads = 0; // 0表示使用CPU核心数的2倍
    
    /**
     * 连接超时时间（毫秒）
     */
    private int connectTimeout = 30000;
    
    /**
     * SO_BACKLOG参数
     */
    private int backlog = 128;
    
    /**
     * 是否启用TCP_NODELAY
     */
    private boolean tcpNodelay = true;
    
    /**
     * 是否启用SO_KEEPALIVE
     */
    private boolean keepAlive = true;
    
    /**
     * 是否启用SO_REUSEADDR
     */
    private boolean reuseAddr = true;
    
    /**
     * 读取空闲时间（秒）
     */
    private int readerIdleTime = 60;
    
    /**
     * 写入空闲时间（秒）
     */
    private int writerIdleTime = 60;
    
    /**
     * 全部空闲时间（秒）
     */
    private int allIdleTime = 60;
    
    /**
     * 最大消息长度
     */
    private int maxMessageLength = 4096;
    
    /**
     * 是否启用SSL
     */
    private boolean sslEnabled = false;
    
    /**
     * SSL密钥库路径
     */
    private String sslKeystore;
    
    /**
     * SSL密钥库密码
     */
    private String sslKeystorePassword;
    
    // Getter和Setter方法
    public int getPort() {
        return port;
    }
    
    public void setPort(int port) {
        this.port = port;
    }
    
    public String getHost() {
        return host;
    }
    
    public void setHost(String host) {
        this.host = host;
    }
    
    public int getBossThreads() {
        return bossThreads;
    }
    
    public void setBossThreads(int bossThreads) {
        this.bossThreads = bossThreads;
    }
    
    public int getWorkerThreads() {
        return workerThreads;
    }
    
    public void setWorkerThreads(int workerThreads) {
        this.workerThreads = workerThreads;
    }
    
    public int getConnectTimeout() {
        return connectTimeout;
    }
    
    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }
    
    public int getBacklog() {
        return backlog;
    }
    
    public void setBacklog(int backlog) {
        this.backlog = backlog;
    }
    
    public boolean isTcpNodelay() {
        return tcpNodelay;
    }
    
    public void setTcpNodelay(boolean tcpNodelay) {
        this.tcpNodelay = tcpNodelay;
    }
    
    public boolean isKeepAlive() {
        return keepAlive;
    }
    
    public void setKeepAlive(boolean keepAlive) {
        this.keepAlive = keepAlive;
    }
    
    public boolean isReuseAddr() {
        return reuseAddr;
    }
    
    public void setReuseAddr(boolean reuseAddr) {
        this.reuseAddr = reuseAddr;
    }
    
    public int getReaderIdleTime() {
        return readerIdleTime;
    }
    
    public void setReaderIdleTime(int readerIdleTime) {
        this.readerIdleTime = readerIdleTime;
    }
    
    public int getWriterIdleTime() {
        return writerIdleTime;
    }
    
    public void setWriterIdleTime(int writerIdleTime) {
        this.writerIdleTime = writerIdleTime;
    }
    
    public int getAllIdleTime() {
        return allIdleTime;
    }
    
    public void setAllIdleTime(int allIdleTime) {
        this.allIdleTime = allIdleTime;
    }
    
    public int getMaxMessageLength() {
        return maxMessageLength;
    }
    
    public void setMaxMessageLength(int maxMessageLength) {
        this.maxMessageLength = maxMessageLength;
    }
    
    public boolean isSslEnabled() {
        return sslEnabled;
    }
    
    public void setSslEnabled(boolean sslEnabled) {
        this.sslEnabled = sslEnabled;
    }
    
    public String getSslKeystore() {
        return sslKeystore;
    }
    
    public void setSslKeystore(String sslKeystore) {
        this.sslKeystore = sslKeystore;
    }
    
    public String getSslKeystorePassword() {
        return sslKeystorePassword;
    }
    
    public void setSslKeystorePassword(String sslKeystorePassword) {
        this.sslKeystorePassword = sslKeystorePassword;
    }
    
    @Override
    public String toString() {
        return "TcpServerConfig{" +
                "port=" + port +
                ", host='" + host + '\'' +
                ", bossThreads=" + bossThreads +
                ", workerThreads=" + workerThreads +
                ", connectTimeout=" + connectTimeout +
                ", backlog=" + backlog +
                ", tcpNodelay=" + tcpNodelay +
                ", keepAlive=" + keepAlive +
                ", readerIdleTime=" + readerIdleTime +
                ", writerIdleTime=" + writerIdleTime +
                ", allIdleTime=" + allIdleTime +
                ", maxMessageLength=" + maxMessageLength +
                ", sslEnabled=" + sslEnabled +
                '}';
    }
}